<?php

namespace app\index\controller\tan;

use think\Controller;
use think\Request;
use think\Session;
use app\index\model\tan\Store as S;
/**
 * 门店管理
 */
class Store extends Controller
{
    use UploadFileTrait;


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(S $s)
    {
        ReturnData::ok($s->where([
            'business_id'=>Session::get("user.id"),
            "state"=>0,
        ]) ->order("id desc")-> paginate());
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 创建门店
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request,S $s)
    {
        $rule=[
            "store_name"=>"require|chsDash",
            "store_description"=>"max:255",
        ];
       $ret= $this->validate($request->post() ,$rule ,["store_name.require"=>"名称必填",
           "store_name.chsDash"=>"名称只能是汉字、字母、数字和下划线_及破折号-","store_description.max"=>"描述最多255字符"
        ]);
//        $d=$this->SetUploadDir("tjx")->Uploads("store_logurl");
        ReturnData::IfTrue($ret, 11101);
        $data = Eliminate::E($request->post(), $rule);
        $data['business_id']=Session::get("user.id");
        $data['store_logurl']=$this->SetUploadDir("tjx")->Uploads_("store_logurl");
        $data['createtime']=date("Y-m-d H:i:s");
        $data['state']=0;
        $s->insert($data);
        ReturnData::ok();
       
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 
     * 保存更新的资源
     *
     * @return \think\Response
     * @param Request $request
     * @param S $s
     * @param type $id  门店id
     */
    public function update(Request $request,S $s, $id)
    {
        $rule=[
            "store_name"=>"require|chsDash",
            "store_description"=>"max:255",
        ];
       $ret= $this->validate($request->post() ,$rule ,["store_name.require"=>"名称必填",
           "store_name.chsDash"=>"名称只能是汉字、字母、数字和下划线_及破折号-","store_description.max"=>"描述最多255字符"
        ]);
        ReturnData::IfTrue($ret, 11105);
        $filename=$this->SetUploadDir("tjx")->Uploads_("store_logurl");
        $data = Eliminate::E($request->post(), $rule);
        if($filename){
            $data['store_logurl']=$filename;
        }
        ReturnData::exit_( $s->where([
            "id"=>$id,
            'business_id'=>Session::get("user.id"),            
        ])->update($data)  , 11104);
    }

    /**
     * 删除指定资源
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(S $s,$id)
    {
        //
        ReturnData::exit_(
                $s->where([
                        'business_id'=>Session::get("user.id"),
                        "state"=>0,
                        "id"=>$id
                    ])->update(["state"=>1,])
                ,
                11103
                ,
                [],
                "删除失败，无权操作或意见被删除"
            );
    }
}
